package pers.duorhs.common.config;

import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import com.corundumstudio.socketio.store.RedissonStoreFactory;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Duorhs
 * @date 2022/5/7 11:15
 */

@Configuration
@EnableConfigurationProperties(SocketProperties.class)
public class SocketIoServerConfig {
    private final SocketProperties socketProperties;
    private final RedissonClient redissonClient;

    @Autowired
    public SocketIoServerConfig(SocketProperties socketProperties, RedissonClient redissonClient) {
        this.socketProperties = socketProperties;
        this.redissonClient = redissonClient;
    }

    @Bean
    public SocketIOServer socketIOServer() {
        com.corundumstudio.socketio.Configuration configuration = new com.corundumstudio.socketio.Configuration();
        configuration.setHostname(socketProperties.getHost());
        configuration.setPort(socketProperties.getPort());
        configuration.setStoreFactory(new RedissonStoreFactory(redissonClient));
        SocketIOServer socketIOServer = new SocketIOServer(configuration);
        socketIOServer.start();
        return socketIOServer;
    }

    @Bean
    public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketIOServer) {
        return new SpringAnnotationScanner(socketIOServer);
    }
}
